
package com.htj.erp.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.htj.erp.dao.HRecvAddressDao;
import com.htj.erp.dao.mybatis.HRecvAddressMapper;
import com.htj.erp.exception.BusinessException;
import com.htj.erp.model.jpa.HRecvAddress;
import com.htj.erp.model.mybatis.HRecvAddressPO;
import com.htj.erp.util.BaiduMapUtil;
import com.htj.erp.util.ExcelUtil;
import com.htj.erp.vo.HRecvAddressVO;
import com.htj.erp.vo.LocationVo;
import com.kedacom.security.service.GenericCpsService;
import com.kedacom.utils.ArrayUtil;
import com.kedacom.utils.StringUtil;
/**
 * 
 *
 * 
 * HRecvAddressService
 * h_recv_address service 处理类
 * 这个基本类一般继承com.kedacom.security.service.GenericCpsService
 * 如果自己实现转换类，通过重写getXXConveter方法。
 * chenghongtao
 * 2016-11-04 14:23:34
 * HTJ
 * @version 1.0.0
 *
 */
@Service
public class HRecvAddressService extends GenericCpsService<HRecvAddress,HRecvAddressPO,HRecvAddressVO,Long>  {

    
	@Resource
	private HRecvAddressDao hRecvAddressDao;
	
	@Resource
	private HRecvAddressMapper hRecvAddressMapper;
	
	/**
	 * 获取jpa的dao
	 */
	@Override
	protected  HRecvAddressDao getEntityJpaDao ()
	{
		return this.hRecvAddressDao;
	}
	 
	  
	/**
	 * 获取 mubatis 的dao
	 */
	@Override
	protected  HRecvAddressMapper getEntityDao ()
	{
		return this.hRecvAddressMapper;
	}
	
	
	
	/**
	 * 
	 * saveOrUpdate 保存或者更新
	 * @param hRecvAddressVO
	 * @return
	 *HRecvAddressVO
	 * @exception
	 * @since  1.0.0
	 */
	public HRecvAddressVO saveOrUpdate(HRecvAddressVO hRecvAddressVO) throws Exception
	{
		
		LocationVo vo = new LocationVo();
		vo.setAddress(hRecvAddressVO.getCusAddress());
		vo.setName(hRecvAddressVO.getCusName());
		vo.setRegion(hRecvAddressVO.getArea());
		if(!Integer.valueOf(1).equals(hRecvAddressVO.getUserDefTude()))
		{
			String[] ll = BaiduMapUtil.determineLngLat(vo);
			if(ll != null)
			{
				hRecvAddressVO.setLongitude(ll[0]);
				hRecvAddressVO.setLatitude(ll[1]);
			}
		}
	    if(hRecvAddressVO.getId() == null || hRecvAddressVO.getId() == 0L)
	    {
	        return this.add(hRecvAddressVO);
	    }
	    else
	    {
	        return this.update(hRecvAddressVO);
	    }
	    
	}
	
	
	public void updateLatLong(String ids) throws Exception
	{
		List<HRecvAddress> rlist = this.getEntityJpaDao().findByIds(ArrayUtil.convertArray(ids.split(",")));
		for(HRecvAddress r : rlist)
		{
			try{
			LocationVo vo = new LocationVo();
			vo.setAddress(r.getCusAddress());
			vo.setName(r.getCusName());
			vo.setRegion("苏州市");
			try{
				String[] ll = BaiduMapUtil.determineLngLat(vo);
				if(ll != null)
				{
					r.setLongitude(ll[0]);
					r.setLatitude(ll[1]);
				}
			}catch(Exception ex)
			{
				ex.printStackTrace();
				r.setLongitude("");
				r.setLatitude("");
			}
			}catch(Exception ex){}
		}
		
		getEntityJpaDao().save(rlist);
	}
	
	public void importFile() throws Exception
	{
		
		XSSFWorkbook  rwb = new XSSFWorkbook(new File("D:\\HTJ\\订单\\全区网点信息表(2016年10月)-1.xlsx"));
		try{
		XSSFSheet xs = rwb.getSheetAt(0);
		List<HRecvAddress> list = new ArrayList<HRecvAddress>();
		
		int i = 2;
		while(true)
		{
			XSSFRow row  = xs.getRow(i);
			if(row == null || row.getCell(0) == null)
				break;;
			String idx = row.getCell(0).getRawValue();
			if(StringUtil.isEmpty(idx))
			{
				break;
			}
			String code = ExcelUtil.getCellValue(row.getCell(1));
			String area =  ExcelUtil.getCellValue(row.getCell(2));
			String name  = ExcelUtil.getCellValue(row.getCell(3));
			String addr = ExcelUtil.getCellValue(row.getCell(4));
			String tel = ExcelUtil.getCellValue(row.getCell(5));
			String psn = ExcelUtil.getCellValue(row.getCell(6));
			String phone = ExcelUtil.getCellValue(row.getCell(7));
			HRecvAddress ha = new HRecvAddress();
			ha.setArea(area);
			ha.setCellphone(phone);
			ha.setContractor(psn);
			ha.setCusAddress(addr);
			ha.setCusCode(code);
			ha.setCusName(name);
			ha.setTelphone(tel);
			list.add(ha);
			i++;
		}
		this.getEntityJpaDao().save(list);
		} 
		finally{
			rwb.close();
		}
	}
	
	/**
	 * 通过地址和名称对应，不做模糊匹配，可能匹配错误，就不行了，如果不对，那么该地址。
	 * @param name
	 * @param address
	 * @return
	 */
	public HRecvAddress findByNameOrAddress(String name,String address)
	{
		List<HRecvAddress> halist = this.getEntityJpaDao().findByCusName(name);
		if(halist.size() == 1)
			return halist.get(0);
		halist = this.getEntityJpaDao().findByCusAddress(address);
		if(halist.size() == 1)
			return halist.get(0);
	
		return null;
	}
	
	public HRecvAddressPO findByNameAndArea(String name,String area)
	{
		List<HRecvAddressPO> halist = this.getEntityDao().findByNameAndArea(name, area);
		if(halist.size() == 1)
			return halist.get(0);
		return null;
	}
	
	public HRecvAddress findByName(String name) throws Exception
	{
		List<HRecvAddress> halist = this.getEntityJpaDao().findByCusName(name);
		if(halist.size() == 1)
			return halist.get(0);
		return null;
	}
	public HRecvAddressPO determinRecvAddress(String... keyword)
	{
		List<HRecvAddressPO> list = null;
		for(String k : keyword)
		{
			if(StringUtil.isNotEmpty(k))
			{
				list = this.getEntityDao().getByKeyword(k);
			}
			if(list.size() == 1)
				return list.get(0);
		}
		for(String k : keyword)
		{
			if(StringUtil.isNotEmpty(k))
			{
				list = this.getEntityDao().locateByKeyword(k);
			}
			if(list.size() == 1)
				return list.get(0);
		}
		return null;
		
	}
	
	
	@Override
	public void delById(Long id) throws Exception {
		Integer num = this.getEntityDao().getUsed(id);
		if(num > 0)
			throw new BusinessException("产品已经被使用无法删除");
		super.delById(id);
	}


	@Override
	public void delByIds(Long[] ids) throws Exception {
		for(Long id : ids)
			delById(id);
	}
}
